package com.hkg.springcloud.controller;

import com.hkg.springcloud.ResultUtil.CommonResult;
import com.hkg.springcloud.entities.Payment;
import com.hkg.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName : PaymentController
 * @Author : 坏老爷
 * @Date: 2020-03-21 18:57
 */
@RestController
@Slf4j
public class PaymentController8001 {

    @Autowired
    private PaymentService paymentService;
    @Resource
    private DiscoveryClient discoveryClient;
    @Value("${server.port}")
    private String serverPort;

    /**
     * 添加支付订单
     *
     * @return
     */
    @PostMapping("/payment/create")
    public CommonResult<Payment> create(@RequestBody Payment payment) {//如何其他服务调用此接口，需使用@RequestBody接收
        // Payment payment = new Payment("槐家大厦");
        int result = paymentService.create(payment);
        CommonResult<Payment> commonResult;
        if (result > 0) {
            commonResult = new CommonResult<Payment>(200, "添加成功,serverPort=" + serverPort, payment);
            log.info("添加成功：记录数为={}" + commonResult);
        } else {
            commonResult = new CommonResult<>(444, "添加失败", null);
            log.info("添加失败" + commonResult);
        }
        return commonResult;
    }

    /**
     * 查询订单
     *
     * @return
     */
    @PostMapping("/payment/query/{id}")
    public CommonResult<Payment> query(@PathVariable("id") Long id) {
        Payment payment = paymentService.queryPaymentById(id);
        CommonResult<Payment> commonResult;
        if (payment != null) {
            commonResult = new CommonResult<>(200, "查询成功,serverPort=" + serverPort, payment);
            log.info("查询成功：内容为={}" + commonResult);
        } else {
            commonResult = new CommonResult<>(444, "查询失败", null);
            log.info("没有该信息" + commonResult);
        }
        return commonResult;
    }

    /**
     * 取消订单
     */
    @PostMapping("/payment/delete/{id}")
    public CommonResult<Payment> deleteOrder(@PathVariable Long id) {
        Payment payment = paymentService.queryPaymentById(id);
        if (payment != null || "".equals(payment)) {
            int delete = paymentService.delete(id);
            CommonResult<Payment> commonResult;
            if (delete > 0) {
                commonResult = new CommonResult<>(200, "已取消订单,serverPort=" + serverPort, payment);
                log.info("取消订单成功" + commonResult);
            } else {
                commonResult = new CommonResult<>(444, "没有该订单，请重新确认", null);
                log.info("取消失败" + commonResult);
            }
            return commonResult;
        } else {
            CommonResult<Payment> commonResult = new CommonResult<>(444, "没有该订单信息", null);
            log.info("没有该订单信息");
            return commonResult;
        }
    }

    /**
     * 查询所有支付订单
     *
     * @return
     */
    @PostMapping("/payment/queryAll")
    public CommonResult<List<Payment>> queryAll() {
        List<Payment> payments = paymentService.queryAll();
        CommonResult<List<Payment>> listCommonResult;
        if (payments.size() > 0) {
            log.info("查询结果" + payments);
            listCommonResult = new CommonResult<>(200, "查询出所有结果,serverPort=" + serverPort, payments);
        } else {
            listCommonResult = new CommonResult<>(444, "无数据", null);
            log.info("无数据");
        }
        return listCommonResult;
    }

    @PostMapping("payment/deleteAll")
    public CommonResult<List<Payment>> deleteAll() {
        List<Payment> payments = paymentService.queryAll();
        CommonResult<List<Payment>> commonResult;
        if (payments.size() > 0) {
            int i = paymentService.deleteAll();
            commonResult = new CommonResult<>(200, "已删除所有数据,serverPort=" + serverPort, payments);
            log.info("删除所有数据" + commonResult);
        } else {
            commonResult = new CommonResult<>(444, "查询无数据,不需要清除", null);
        }
        return commonResult;
    }

    @PostMapping(value = "/payment/discovery")
    public Object discovery() {
        List<String> services = discoveryClient.getServices();
        for (String element : services) {
            log.info("***** element:" + element);
        }
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        for (ServiceInstance instance : instances) {
            log.info(instance.getServiceId() + "\t" + instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri());
        }
        return this.discoveryClient;
    }
}